//
//  CenterpageSearchResultViewController.m
//  ShangHaiTuJI
//
//  Created by xiaoxiangtuji on 2017/10/28.
//  Copyright © 2017年 xiaoxiangtuji. All rights reserved.
//

#import "CenterpageSearchResultViewController.h"
#import "SegmentTapView.h"
#import "FlipTableView.h"
#import "SearchInstituViewController.h"
#import "SearchActivityViewController.h"
#import "EVNCustomSearchBar.h"
#import "InstituteModel.h"
@interface CenterpageSearchResultViewController ()<FlipTableViewDelegate,SegmentTapViewDelegate,EVNCustomSearchBarDelegate>

@property (nonatomic, strong)SegmentTapView *segment;
@property (nonatomic, strong)FlipTableView *flipView;
@property(nonatomic,strong)NSMutableArray *controllsArray;
@property(nonatomic,strong)EVNCustomSearchBar * searchBar;
@property(nonatomic,assign) NSInteger cataType;
@property(nonatomic,strong)NSMutableArray *dataArray;
@end

@implementation CenterpageSearchResultViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.cataType = 0;
    [self initSearchBar];
    [self initSegment];
    // Do any additional setup after loading the view.
}
/**
 *  分段选择的标题设置
 *
 */
-(void)initSegment{
    self.segment = [[SegmentTapView alloc] initWithFrame:CGRectMake(0,64 , WIDTH, 40) withDataArray:[NSArray arrayWithObjects:@"机构",@"活动", nil] withFont:20];
    self.segment.delegate = self;
    self.segment.lineColor = COLOR_HEX(0xf9bc3d);
    self.segment.titleFont = scaleLeftAndRight(15);
    self.segment.textNomalColor = [UIColor lightGrayColor];
    self.segment.textSelectedColor = COLOR_HEX(0xf9bc3d);
    [self.view addSubview:self.segment];
    [self initFlipTableView];
}
-(void)initFlipTableView{
    SearchInstituViewController *instituVc = [[SearchInstituViewController alloc] init];
    instituVc.navBlock = ^(UIViewController *vc) {
        [self.navigationController pushViewController:vc animated:YES];
    };
    SearchActivityViewController *activityVc = [[SearchActivityViewController alloc] init];
    [self.controllsArray addObject:instituVc];
    [self.controllsArray addObject:activityVc];
    self.flipView = [[FlipTableView alloc] initWithFrame:CGRectMake(0,64 + 40, WIDTH, HEIGHT - (64 + 40)) withArray:_controllsArray];
    self.flipView.delegate = self;
    [self.view addSubview:self.flipView];
    
}

#pragma mark -------- select Index
-(void)selectedIndex:(NSInteger)index
{
    NSLog(@"选中的标志%ld",index);
    self.cataType = index ;
    [self.flipView selectIndex:index];
    
}
-(void)scrollChangeToIndex:(NSInteger)index
{
    self.cataType = index - 1;
    NSLog(@"%ld",index);
    [self.segment selectIndex:index];
}
-(NSMutableArray *)controllsArray{
    if (_controllsArray == nil) {
        _controllsArray = [[NSMutableArray alloc] init];
    }
    return _controllsArray;
}
#pragma mark: 设置顶部导航搜索部分
- (void)initSearchBar
{
    self.navigationItem.titleView = self.searchBar;
    if (@available(iOS 11.0, *))
    {
        [self.searchBar.heightAnchor constraintLessThanOrEqualToConstant:44].active = YES;
    }
    else
    {
        
    }
    
    
}

#pragma mark: getter method EVNCustomSearchBar
- (EVNCustomSearchBar *)searchBar
{
    if (!_searchBar)
    {
        _searchBar = [[EVNCustomSearchBar alloc] initWithFrame:CGRectMake(0, 20, WIDTH, 44)];
        
        _searchBar.backgroundColor = [UIColor clearColor]; // 清空searchBar的背景色
        //        _searchBar.iconImage = [Helper imagesNamedFromCustomBundle:@"EVNCustomSearchBar.png"];
        _searchBar.iconAlign = EVNCustomSearchBarIconAlignLeft;
        [_searchBar setPlaceholder:@"请输入关键字"];  // 搜索框的占位符
        //        _searchBar.placeholderColor = TextGrayColor;
        _searchBar.delegate = self; // 设置代理
        [_searchBar sizeToFit];
        [_searchBar becomeFirstResponder];
    }
    return _searchBar;
}

#pragma mark: EVNCustomSearchBar delegate method
- (BOOL)searchBarShouldBeginEditing:(EVNCustomSearchBar *)searchBar
{
    NSLog(@"class: %@ function:%s", NSStringFromClass([self class]), __func__);
    return YES;
}

- (void)searchBarTextDidBeginEditing:(EVNCustomSearchBar *)searchBar
{
    NSLog(@"class: %@ function:%s", NSStringFromClass([self class]), __func__);
}

- (BOOL)searchBarShouldEndEditing:(EVNCustomSearchBar *)searchBar
{
    NSLog(@"class: %@ function:%s", NSStringFromClass([self class]), __func__);
    return YES;
}

- (void)searchBarTextDidEndEditing:(EVNCustomSearchBar *)searchBar
{
    NSLog(@"class: %@ function:%s", NSStringFromClass([self class]), __func__);
}

- (void)searchBar:(EVNCustomSearchBar *)searchBar textDidChange:(NSString *)searchText
{
    NSLog(@"class: %@ function:%s", NSStringFromClass([self class]), __func__);
    NSLog(@"搜索到的字%@",searchText);
}

- (BOOL)searchBar:(EVNCustomSearchBar *)searchBar shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
    NSLog(@"class: %@ function:%s", NSStringFromClass([self class]), __func__);
    return YES;
}

- (void)searchBarSearchButtonClicked:(EVNCustomSearchBar *)searchBar
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSString *url = ServerIp@"/indexApp/search";
    NSString *category = [NSString stringWithFormat:@"%ld",self.cataType];
    NSDictionary *param = [NSDictionary dictionaryWithObjectsAndKeys:searchBar.text,@"institution_name",category,@"category", nil];
    NSLog(@"参数的值%@",param);
    [manager POST:url parameters:param progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        [self.dataArray removeAllObjects];
        NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"错误信息%@",jsonDic[@"msg"]);
        NSLog(@"%@",jsonDic);
        NSArray *listArra = jsonDic[@"list"];
        if (listArra.count == 0) {
//            [self.collectionView reloadData];
        }else{
            for (NSDictionary *dic in listArra) {
                InstituteModel *model = [InstituteModel modelWithDictionary:dic];
                [self.dataArray addObject:model];
            }
            [[NSNotificationCenter defaultCenter] postNotificationName:@"reloadSearchData" object:nil userInfo:@{@"dataArray":self.dataArray}];
//            NSLog(@"所有搜索数据%@",self.dataArray);
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        NSLog(@"%@",error);
    }];

    NSLog(@"class: %@ function:%s", NSStringFromClass([self class]), __func__);
}

- (void)searchBarCancelButtonClicked:(EVNCustomSearchBar *)searchBar
{
    NSLog(@"class: %@ function:%s", NSStringFromClass([self class]), __func__);
}

-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
